package com.mls.fivedistrict.controller.backend;


import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.pojo.NewsNewsTag;
import com.mls.fivedistrict.pojo.NewsTag;
import com.mls.fivedistrict.server.NewsTagService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 新闻标签 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-06-28
 */
@Slf4j
@RestController
@RequestMapping("/api/manager")
public class NewsTagController {
    @Autowired
    private NewsTagService newsTagService;

    @GetMapping("/v1/newsTag/all")
    public ServiceResponse getAll() {
        List newsList = newsTagService.selectList(Condition.create());
        return ServiceResponse.createSuccessResponse(newsList);
    }


    @RequestMapping("/v1/newsTags")
    public ServiceResponse getNewsList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
//        List<HotWord> hotWordList = hotWordService.selectList(pageFilter.getWrapper());
        List<NewsTag> newsTagList = newsTagService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo<NewsTag>(newsTagList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    @GetMapping("/v1/newsTag/{newsTagId}")
    public ServiceResponse getList(@PathVariable("newsTagId") long newsTagId) {
        NewsTag newsTag = newsTagService.selectById(newsTagId);
        return ServiceResponse.createSuccessResponse(newsTag);
    }

    @PutMapping("v1/newsTag/{newsTagId}")
    public ServiceResponse updateById(@PathVariable("newsTagId") long newsTagId, @RequestBody NewsTag newsTag) {
        newsTag.setId(newsTagId);
        boolean update = newsTagService.updateById(newsTag);
        if (update) {
            return ServiceResponse.createSuccessResponse("编辑成功");
        }
        return ServiceResponse.createSuccessResponse("编辑失败");
    }

    @PostMapping("v1/newsTag")
    public ServiceResponse add(@RequestBody NewsTag newsTag) {
        //1所有的添加操作统一校验
        BeanValidator.check(newsTag);
        boolean insert = newsTagService.insert(newsTag);
        if (insert) {
            return ServiceResponse.createSuccessResponse("添加成功");
        }
        return ServiceResponse.createSuccessResponse("添加失败");
    }

    @DeleteMapping("v1/newsTag/{newsTagId}")
    public ServiceResponse deleteById(@PathVariable("newsTagId") Long newsTagId) {
//        boolean insert = newsTagService.deleteById(newsTagId);
        NewsTag newsTag = new NewsTag();
        newsTag.setId(newsTagId);
        newsTag.setIsDelete(true);
        boolean update = newsTagService.updateById(newsTag);
        if (update) {
            return ServiceResponse.createSuccessResponse("删除成功");
        }
        return ServiceResponse.createErrorResponse("删除失败");
    }
}

